package com.yoomiito.app.adapter.home.reommend

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.yoomiito.app.R
import com.yoomiito.app.model.home.HomeHotActData
import com.yoomiito.app.utils.ImageLoader

class HotActTitleAdapter (private val context: Context, private val layoutHelper: LayoutHelper, private var data: HomeHotActData?,var onItemClickListener:(HomeHotActData)->Unit)
    : DelegateAdapter.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_hot_act_title, p0, false)
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int = if (data==null) 0 else 1

    override fun onCreateLayoutHelper(): LayoutHelper = layoutHelper

    override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
        val holder = p0 as ItemViewHolder
        ImageLoader.getInstance().into(context,data?.imageUrl,holder.iv,R.drawable.default_banner_1_long)
        holder.iv.setOnClickListener {
            onItemClickListener(data!!)
        }
    }

    fun setNewDatas(data: HomeHotActData?){
        this.data = data
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int) = 15

    private class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iv: ImageView = itemView.findViewById(R.id.iv)
    }
}